package guns
{
	/**
	 * ...
	 * @author ...
	 */
	import flash.display.*;
	import Main;
	import MovingCircle;
	import flash.events.*;
	import emb.SoundLib;
	
	public class Gun 
	{
		protected var owner:MovingCircle;
		public var triggerHappy:Boolean;
		public var damage:int;
		public var range:int;
		public var reload:int;
		public var shell:int;
		public var capacity:int;
		public var spread:Number;
		protected var n:int;
		protected var r:int;
		private var c:int;
		public var FPS:int;//Frames per shot (Not the frames, but Simulations)
		public var BPS:int;//Bullets per shot
		public function Gun(pl:MovingCircle) 
		{
			owner = pl;
			//Main.Instance().stage.addEventListener(Event.ENTER_FRAME, think);
			BPS = 1;
			FPS = 10;
			n = (Math.random() * FPS);
			c = 0;
			r = 0;
			capacity = 1;   
			reload = 1;
			spread = 0;
		}
		
		public function Shoot():void
		{
			var shot:Bullet = null;
			if (!(n % FPS))
			{
				if (c != capacity)
				{
					if (!r)
					{
						if (!triggerHappy) return;
						for (var i:int = 0; i < BPS; i++)
						{
							c++;
							if (owner as Player || owner.brainDead)
								shot = new Bullet(owner.rotation, spread);
							else
								shot = Bullet.EnemyBullet(owner.rotation);
							shot.SetStartPosition(owner.x, owner.y);
							shot.sa.Sync();
							shot.sb.Sync();
							if (Math.abs(shot.x) < 15) throw "Bad bullet created!";
							Main.Instance().AddCollidingChild(shot);
						}
						SoundLib.Instance().PlaySound(owner.x, owner.y, SoundLib.SShotUp);
						//Main.Instance().Puff(owner.x, owner.y);
					}
					else
					{
						if (r == reload)
							r = 0;
						else
							r++;
					}
				}
				else
				{
					c = 0;
					r++;
				}
			}
			n++;
		}
		public function think(event:Event):void
		{
			Shoot();
		}
		
	}

}